package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupMapper;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckItemDao checkItemDao;
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult page(QueryPageBean queryPageBean) {
        log.info("[检查项-分页查询]data:{}",queryPageBean);
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //分页查询数据库
        Page<CheckGroup> page = checkGroupMapper.select(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增
     * @param checkGroup
     */
    public void add(CheckGroup checkGroup,Integer[] checkitemIds) {
        Integer groupId = checkGroupMapper.insert(checkGroup);
        for (Integer checkitemId : checkitemIds) {
            CheckItem checkItem = checkItemDao.selectById(checkitemId);
            String name = checkItem.getName();
            checkGroupMapper.insertGroupItem(groupId,checkitemId,name);
        }
    }

    /**
     * 通过groupId获取itemId数组
     * @param id
     * @return
     */
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        List<Integer> ids = checkGroupMapper.findCheckItemIdsByCheckGroupId(id);
        return ids;
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public CheckGroup fingById(Integer id) {
        CheckGroup checkGroup = checkGroupMapper.findById(id);
        return checkGroup;
    }

    /**
     * 编辑
     * @param checkGroup
     * @param checkitemIds
     */
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupMapper.updateGroup(checkGroup);
        Integer groupId = checkGroup.getId();
        //清空相关关系表
        checkGroupMapper.deleteGroupItem(groupId);
        for (Integer checkitemId : checkitemIds) {
            CheckItem checkItem = checkItemDao.selectById(checkitemId);
            String name = checkItem.getName();
            checkGroupMapper.insertGroupItem(groupId,checkitemId,name);
        }
    }

    /**
     * 删除检查组
     * @param id
     */
    public void delete(Integer id) {
        checkGroupMapper.deleteGroup(id);
        //清空相关关系表
        checkGroupMapper.deleteGroupItem(id);
    }

    /**
     * 插叙所有
     * @return
     */
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroups = checkGroupMapper.findAll();
        return checkGroups;
    }
}
